package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageBean page(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        //执行查询
        List<Student> studentList = studentMapper.list(studentQueryParam);
        Page<Student> p = (Page<Student>) studentList;
        return new PageBean(p.getTotal(),p.getResult());
    }

    @Override
    public void delete(List<Integer> ids) {
        studentMapper.deleteById(ids);
    }

    /**
     * 添加学生业务处理
     * @param student
     */
    @Override
    public void add(Student student) {
        //1.调用studentMapper保存学生的基本信息
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.add(student);
    }

    /**
     * 根据id查询学生信息的业务逻辑
     * @param id
     * @return
     */
    @Override
    public Student getById(Integer id) {
        //1.调用studentMapper查询基本信息
        return studentMapper.getById(id);
    }

    /**
     * 修改学员功能
     * @param student
     */
    @Override
    public void update(Student student) {
        //1.调用studentMapper 修改
          studentMapper.update(student);
    }

    /**
     * 违纪分数和次数的修改
     * @param id
     * @param score
     */
    @Override
    public void updateViolation(Integer id, Integer score) {
        studentMapper.updateViolation(id,score);
    }
}
